﻿Imports System.Data.SqlClient

Public Class clsALMACENES
    Private mintIDAlmacen As Integer
    Public Property IDALMACEN() As Integer
        Get
            Return mintIDAlmacen
        End Get
        Set(ByVal value As Integer)
            mintIDAlmacen = value
        End Set
    End Property
    Public Sub New()
        mintIDAlmacen = 0
    End Sub
    Public Function INSERTA_ALMACEN(ByVal CODIGO As String, ByVal NOMBRE As String, ByVal CLASIF1 As String, ByVal CLASIF2 As String, ByVal CLASIF3 As String, ByVal SAE As String, ByVal NOM_CORTO As String, ByVal ENTRADAS As Integer, ByVal SALIDAS As Integer, ByVal COMPRAS As Integer, ByVal PRODUCCION As Integer, ByVal RESURTE As Integer, ByVal ESTATUS As Integer, ByVal TIPO As Integer, ByVal IDDOMICILIO As Integer, ByVal IDUSUARIO As Integer)
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand("sp_ALMACENES", cnConn)
        Command.CommandType = CommandType.StoredProcedure

        Command.Parameters.Add(New SqlParameter("@CODIGO", SqlDbType.VarChar, 150))
        Command.Parameters("@CODIGO").Value = CODIGO
        Command.Parameters.Add(New SqlParameter("@NOMBRE", SqlDbType.VarChar, 30))
        Command.Parameters("@NOMBRE").Value = NOMBRE
        Command.Parameters.Add(New SqlParameter("@CLASIF1", SqlDbType.VarChar, 100))
        Command.Parameters("@CLASIF1").Value = CLASIF1
        Command.Parameters.Add(New SqlParameter("@CLASIF2", SqlDbType.VarChar, 100))
        Command.Parameters("@CLASIF2").Value = CLASIF2
        Command.Parameters.Add(New SqlParameter("@CLASIF3", SqlDbType.VarChar, 100))
        Command.Parameters("@CLASIF3").Value = CLASIF3
        Command.Parameters.Add(New SqlParameter("@SAE", SqlDbType.VarChar, 100))
        Command.Parameters("@SAE").Value = SAE
        Command.Parameters.Add(New SqlParameter("@NOM_CORTO", SqlDbType.VarChar, 100))
        Command.Parameters("@NOM_CORTO").Value = NOM_CORTO
        Command.Parameters.Add(New SqlParameter("@ENTRADAS", SqlDbType.Int))
        Command.Parameters("@ENTRADAS").Value = ENTRADAS
        Command.Parameters.Add(New SqlParameter("@SALIDAS", SqlDbType.Int))
        Command.Parameters("@SALIDAS").Value = SALIDAS
        Command.Parameters.Add(New SqlParameter("@COMPRAS", SqlDbType.Int))
        Command.Parameters("@COMPRAS").Value = COMPRAS
        Command.Parameters.Add(New SqlParameter("@PRODUCCION", SqlDbType.Int))
        Command.Parameters("@PRODUCCION").Value = PRODUCCION
        Command.Parameters.Add(New SqlParameter("@RESURTE", SqlDbType.Int))
        Command.Parameters("@RESURTE").Value = RESURTE
        Command.Parameters.Add(New SqlParameter("@ESTATUS", SqlDbType.Int))
        Command.Parameters("@ESTATUS").Value = ESTATUS
        Command.Parameters.Add(New SqlParameter("@TIPO", SqlDbType.Int))
        Command.Parameters("@TIPO").Value = TIPO
        Command.Parameters.Add(New SqlParameter("@IDDOMICILIO", SqlDbType.Int))
        Command.Parameters("@IDDOMICILIO").Value = IDDOMICILIO
        Command.Parameters.Add(New SqlParameter("@IDUSUARIO", SqlDbType.Int))
        Command.Parameters("@IDUSUARIO").Value = IDUSUARIO
        Command.Parameters.Add(New SqlParameter("@NOMBRE_USUARIO", SqlDbType.VarChar, 100))
        Command.Parameters("@NOMBRE_USUARIO").Value = UsuarioActivo
        Command.Parameters.Add(New SqlParameter("@NUM_CLASIF1", SqlDbType.Int))
        Command.Parameters("@NUM_CLASIF1").Value = CLASIFICACIONES_ALMACENES.Clasificacion_1
        Command.Parameters.Add(New SqlParameter("@NUM_CLASIF2", SqlDbType.Int))
        Command.Parameters("@NUM_CLASIF2").Value = CLASIFICACIONES_ALMACENES.Clasificacion_2
        Command.Parameters.Add(New SqlParameter("@NUM_CLASIF3", SqlDbType.Int))
        Command.Parameters("@NUM_CLASIF3").Value = CLASIFICACIONES_ALMACENES.Clasificacion_3

        Try
            cnConn.Open()
            Dim Lector As SqlDataReader

            Lector = Command.ExecuteReader()
            If Lector.Read() Then
                INSERTA_ALMACEN = Lector("RESULTADO")
                mintIDAlmacen = Lector("IDALMACEN")
            End If
        Catch ex As Exception
            INSERTA_ALMACEN = -1
            MsgBox(ex.Message, MsgBoxStyle.Information, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End Try


    End Function
    Public Function INSERTA_RESUTIDOS(ByVal IDALMACEN_HIJO As Integer, ByVal ES_DEFAULT As Integer) As Integer
        Dim sql As String = "Insert Into ALMACEN_RESURTIDOS VALUES(" & _
                           "@IDALMACEN_PADRE," & _
                           "@IDALMACEN_HIJO," & _
                           "@DEFAULT)"

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand(sql, cnConn)

        Command.Parameters.AddWithValue("IDALMACEN_PADRE", mintIDAlmacen)
        Command.Parameters.AddWithValue("IDALMACEN_HIJO", IDALMACEN_HIJO)
        Command.Parameters.AddWithValue("DEFAULT", ES_DEFAULT)


        cnConn.Open()
        Dim RowsAffected As Integer
        Try
            RowsAffected = Command.ExecuteNonQuery
            If RowsAffected > 0 Then
                cnConn.Close()
                INSERTA_RESUTIDOS = 1
            Else
                If cnConn.State = ConnectionState.Open Then
                    cnConn.Close()
                End If
                INSERTA_RESUTIDOS = 0
            End If
        Catch ex As Exception
            INSERTA_RESUTIDOS = -1
            MsgBox(ex.Message, MsgBoxStyle.Information, "Aviso")
        End Try
    End Function

End Class
